<template>
    <div class="container containerBind">
        <!-- header -->
        <div class="header">
            <div class="header-left">
                <div class="header-box">
                    <p class="header-title">学校名称:</p>
                    <el-select
                        v-model="header.school"
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in header.schoolList"
                            :key="item.school_id"
                            :label="item.school_name"
                            :value="item.school_id"
                        />
                    </el-select>
                </div>
                <div class="header-box">
                    <p class="header-title">用户:</p>
                    <el-input
                        v-model="header.tel"
                        placeholder="用户ID或手机号"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-box">
                    <p class="header-title">店铺或姓名:</p>
                    <el-input
                        v-model="header.keyword"
                        placeholder="店铺或姓名"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        class="search-ipt"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                label="序号"
                width="100px"
                align="center"
                type="index"
            />
            <el-table-column
                prop="true_name"
                label="用户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student_no"
                label="用户ID"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="phone"
                label="电话号码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="school_name"
                label="学校信息"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.school_name }}-{{ scope.row.college_name }}-{{ scope.row.major_name }}
                </template>
            </el-table-column>
            <el-table-column
                prop="shop_num"
                label="店铺"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span
                        @click="fnGoPage(scope.row)"
                        class="curse-add"
                    >{{ scope.row.shop_num }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="reply_student_count"
                label="服务用户数量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="reply_wait_second_avg"
                label="平均响应时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="操作"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnUnBind(scope.row)"
                    >
                        解绑店铺
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 解绑店铺 -->
        <el-dialog
            title="解绑客服"
            :visible.sync="dialogVisibleUnBind"
            width="580px"
            :close-on-click-modal="false"
            class="dialogVisibleUnBind"
            center
        >
            <el-checkbox-group v-model="checkListKf">
                <div
                    v-for="(item,index) in kfList"
                    :key="index"
                >
                    <el-checkbox
                        :label="item.shop_no"
                        class="el-checkList"
                    >
                        <span>{{ item.shop_no }} - {{ item.company_name }} - {{ item.shop_title }}</span>
                    </el-checkbox>
                </div>
            </el-checkbox-group>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    @click="dialogVisibleUnBind = false">取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    :disabled="ifBind"
                    @click="fnUntying()">解 绑</el-button>
            </span>
        </el-dialog>
        <!-- /解绑店铺 -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->
    </div>
</template>

<script>
export default {
    /**
     * @param  {Array}     tableData        //表单数据源
     * @param  {Object}    page             //分页
     * @param  {String}    school           //学校名称
     * @param  {Array}     schoolList       //城市+学校数据
     * @param  {String}    tel              //手机号码
     */
    data () {
        return {
            ruleForm: {
                first: '',
                second: ''
            },
            ifBind: true,
            student_no: '',
            kfList: [],
            checkListKf: [],
            dialogVisibleUnBind: false,
            header: {
                tel: '',
                schoolList: [],
                school: '',
                keyword: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            shop_no: ''
        };
    },
    watch: {
        checkListKf (val) {
            if (val.length == 0) {
                this.ifBind = true;
            } else {
                this.ifBind = false;
            }
        },
        dialogVisibleUnBind (val) {
            if (!val) {
                this.checkListKf = [];
            }
        }
    },
    filters: {
    },
    created () {
        let query = this.$route.query;

        if (query.shop_no !== 'undefined' || query.shop_no !== undefined) {
            this.shop_no = query.shop_no;
        }

        this.setHeader();
        this.getTableData();
        this.getSchoolName();
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '商城'
            },
            {
                title: "客服管理"
            }]
            );
        },

        /**
         * 获取学校+城市
         */
        getSchoolName () {
            let arr = [];

            this.$post('/student/shop_kf%5Cget_kf_school_list',{
            },resp=>{
                if (resp.code == 1) {
                    resp.data.map(item=>{
                        if (item.school_id === null) {
                            //过滤空值
                        } else {
                            arr.push({
                                school_id: item.school_id,
                                school_name: item.school_name
                            });
                        }
                    });
                    this.header.schoolList = this.header.schoolList.concat(arr);
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_kf%5Cget_list', {
                shop_no: this.shop_no,
                page_id: page || this.page.now_page,
                school_id: this.header.school,
                keyword_student_id: this.header.tel,
                keyword: this.header.keyword
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 解绑店铺
         */
        fnUnBind (row) {
            this.kfList = [];
            this.student_no = row.student_no;
            this.dialogVisibleUnBind = !this.dialogVisibleUnBind;
            this.$post('/student/shop_kf%5Cget_bind_shop_list',{
                student_no: row.student_no
            },resp=> {
                if (resp.code == 1) {
                    if (resp.data.length !== 0) {
                        this.kfList = this.kfList.concat(resp.data.list);
                    }
                }
            });
        },

        /**
         * 确认解绑店铺
         */
        fnUntying () {
            this.$post('/student/shop_kf%5Cun_bind_shop',{
                student_no: this.student_no,
                shop_nos: this.checkListKf.join()
            },resp=> {
                if (resp.code == 1) {
                    this.getTableData();
                    this.dialogVisibleUnBind = !this.dialogVisibleUnBind;
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 进入店铺管理
         */
        fnGoPage (row) {
            this.$router.replace({
                name: 'ShopList',
                query: {
                    student_no: row.student_no
                }
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            padding-bottom: 15px;
            .header-left, .header-right {
                display: flex;
                flex-wrap: wrap;
                height: 32px;
                align-items: center;
            }
            .header-box {
                display: flex;
                margin-right: 20px;
                align-items: center;
                color: #606266;
                margin-bottom: 20px;
                .header-title {
                    margin-right: 10px;
                    white-space: nowrap;
                }
                .btn-import-wrap {
                    margin-left: 15px;
                    display: inline-block;
                    position: relative;
                    overflow: hidden;
                    cursor: pointer;
                    vertical-align: top;
                    .import-input {
                        margin-left: -100%;
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 200%;
                        height: 100%;
                        cursor: pointer;
                        opacity: 0;
                    }
                }
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .el-tag {
        margin: 0 15px 15px 0;
    }
    .el-checkList {
        display: flex;
        margin-bottom: 15px;
    }
    .dialog-title {
        color: #1F2D3D;
        font-size: 16px;
        font-weight: bold;
        width: 100%;
        text-align: center;
        margin-bottom: 40px;
    }
    .dialog-foot {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 44px;
        height: 32px;
        align-items: center;
    }
</style>
<style lang="less">
    .containerBind .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .containerBind .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .containerBind .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .containerBind .dialogVisibleUnBind .el-checkbox-group {
        max-height: 400px;
        overflow-y: auto;
    }
</style>